import { createRouter, createWebHistory } from 'vue-router'
import { useCurrentUserStore } from '@/modules/security/stores/currentUserStores';
import {portalRouter} from '@/modules/portal/router'
import {adminRouter} from '@/modules/admin/router'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      meta: {
        name: '登录',
        noAuth: true
      },
      component: () => import('../../security/views/LoginView.vue')
    },
    ...adminRouter,
    ...portalRouter
  ]
})
router.beforeEach((to, from, next) => {
  const userStore = useCurrentUserStore()
  // 是需要登录路由 并且 没有登录 ,跳转到/login
  if (!to.meta.noAuth && !userStore.isLoggedIn()) {
    next('/login')
  }
  // else if(!to.meta.noAuth) {
  // }
  else {
    next()
  }
})

export default router
